package com.atguigu.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.base.BaseDao;
import com.atguigu.base.BaseServiceImpl;
import com.atguigu.dao.UserFollowDao;
import com.atguigu.entity.UserFollow;
import com.atguigu.service.DictService;
import com.atguigu.service.UserFollowService;
import com.atguigu.vo.UserFollowVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = UserFollowService.class)
@Transactional
public class UserFollowServiceImpl extends BaseServiceImpl<UserFollow> implements UserFollowService {

    @Autowired
    private UserFollowDao userFollowDao;
    @Reference
    private DictService dictService;

    @Override
    protected BaseDao<UserFollow> getEntityDao() {
        return userFollowDao;
    }

    @Override
    public void follow(Long userId, Long houseId) {
        UserFollow userFollow = new UserFollow();
        userFollow.setUserId(userId);
        userFollow.setHouseId(houseId);
        this.userFollowDao.insert(userFollow);
    }

    @Override
    public boolean isFollowed(Long userId, Long houseId) {
        int count = this.userFollowDao.findCountFollow(userId, houseId);
        if (count > 0) {
            return true;
        }
        return false;
    }

    @Override
    public PageInfo<UserFollowVo> findListPage(int pageNum, int pageSize, Long userId) {
        //开启分页
        PageHelper.startPage(pageNum,pageSize);
        //查询当前页信息
        Page<UserFollowVo> page = this.userFollowDao.findListPage(userId);
        //还有其他工作要做的(根据字典项id或者name)
        List<UserFollowVo> list = page.getResult();
        for(UserFollowVo userFollowVo:list){
            //户型：（字典id）
            String houseTypeName = this.dictService.getNameById(userFollowVo.getHouseTypeId());
            userFollowVo.setHouseTypeName(houseTypeName);
            //楼层（字典id）
            String floorName = this.dictService.getNameById(userFollowVo.getFloorId());
            userFollowVo.setFloorName(floorName);
            //朝向：（字典id）
            String directionName = this.dictService.getNameById(userFollowVo.getDirectionId());
            userFollowVo.setDirectionName(directionName);
        }
        //返回数据
        return new PageInfo<>(page);
    }

    @Override
    public void cancelFollow(Long id) {
        userFollowDao.delete(id);
    }
}
